#ifndef __M_UTILs__
#define __M_UTILs__

// 工具类实现
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctime>

namespace wzh
{
    namespace util
    {
        // 1. 获取系统时间
        class Date
        {
        public:
            static size_t nowTime()
            {
                return (size_t)time(nullptr);
            }
        };
        // 2.判断文件是否存在及创建文件夹和获取文件的路径
        class File
        {
        public:
            static bool exists(const std::string &pathname)
            {
                struct stat st;
                if (stat(pathname.c_str(), &st) < 0)
                    return false;
                return true;
                // return (access(pathname.c_str(), F_OK) == 0);
            }

            static std::string path(const std::string &pathname)
            {
                // ./abc/dbc/cdb/a.txt
                size_t pos = pathname.find_last_of("/\\");
                if (pos == std::string::npos)
                    return ".";
                return pathname.substr(0, pos + 1);
            }

            static void createDirectory(const std::string &pathname)
            {
                // ./abc/dbc/cdb/a.txt
                size_t pos = 0, idx = 0;
                while (idx < pathname.size())
                {
                    pos = pathname.find_first_of("/\\", idx);
                    if (pos == std::string::npos)
                    {
                        mkdir(pathname.c_str(), 0777);
                    }
                    std::string parent_dir = pathname.substr(0, pos + 1);
                    if (exists(parent_dir) == true)
                    {
                        idx = pos + 1;
                        continue;
                    }
                    mkdir(parent_dir.c_str(), 0777);
                    idx = pos + 1;
                }
            }
        };
    }
}

#endif